package com.xjm.i1.core.resolver;

import com.xjm.i1.constant.AreaEnum;
import com.xjm.i1.constant.ColorEnum;
import com.xjm.i1.constant.MaxGrayEnum;
import com.xjm.i1.core.struct.location.MaxDataPointMap;
import com.xjm.i1.core.struct.location.Point;
import com.xjm.i1.entity.ColorContrastArea;
import com.xjm.i1.entity.ColorGrayArea;
import com.xjm.i1.core.struct.node.load.I1AreaCollections;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.List;

/**
 * i1原始数据中灰度偏差与阶调解析器
 **/
@Data
public abstract class AbstractColorContrastGrayResolver implements MetaDataResolver {

    // 数据页的区域
    private AreaEnum areaSign;

    // 数据加载坐标映射
    private List<MaxDataPointMap> pointMaps;

    public AbstractColorContrastGrayResolver(AreaEnum areaSign, List<MaxDataPointMap> pointMaps) {
        this.areaSign = areaSign;
        this.pointMaps = pointMaps;
    }

    @Override
    public boolean resolverMatch(AreaEnum areaEnum) {
        return this.areaSign == areaEnum;
    }

    @Override
    public I1AreaCollections resolve(Double[][] metaData) throws NoSuchFieldException, IllegalAccessException {
        int initGrayArea = MaxGrayEnum.values().length;
        int initContrastArea = ColorEnum.getMaxColorEnumOrdered().length;
        I1AreaCollections res = new I1AreaCollections(initGrayArea, initContrastArea);
        for (MaxDataPointMap pointMap : pointMaps) {
            MaxDataPointMap.PointMapType mapType = pointMap.getMapType();
            switch (mapType) {
                case COLOR_CONTRAS:
                    ColorContrastArea bean = new ColorContrastArea();
                    bean.setColorSign(pointMap.getColorSign());
                    bean.setAreaSign(areaSign);
                    resolve0(metaData, pointMap, bean);
                    res.getColorContrastAreas().add(bean);
                    break;
                case COLOR_GRAY:
                    ColorGrayArea bean1 = new ColorGrayArea();
                    bean1.setGraySign(pointMap.getGraySign());
                    bean1.setAreaSign(areaSign);
                    resolve0(metaData, pointMap, bean1);
                    res.getColorGrayAreas().add(bean1);
                    break;
            }
        }
        return res;
    }

    void resolve0(Double[][] metaData, MaxDataPointMap pointMap, ColorContrastArea bean)  throws NoSuchFieldException, IllegalAccessException {
        for (Point point : pointMap.getPoints()) {
            double val = metaData[point.getX()][point.getY()];
            String mapV = point.getMapValue();

            Field declaredField = bean.getClass().getDeclaredField(mapV);
            declaredField.setAccessible(true);
            declaredField.set(bean, val);
        }
    }

    void resolve0(Double[][] metaData, MaxDataPointMap pointMap, ColorGrayArea bean)  throws NoSuchFieldException, IllegalAccessException {
        for (Point point : pointMap.getPoints()) {
            double val = metaData[point.getX()][point.getY()];
            String mapV = point.getMapValue();

            Field declaredField = bean.getClass().getDeclaredField(mapV);
            declaredField.setAccessible(true);
            declaredField.set(bean, val);
        }
    }
}